//
//  JigSawFullScreenMovieView.m
//  JigSawKiosk
//
//  Created by Stulberg on 10-10-17.
//  Copyright 2010 FruitonLabs. All rights reserved.
//

#import "JigSawFullScreenMovieView.h"


@implementation JigSawFullScreenMovieView

@synthesize rootLayer, flMovieLayer, flMovie ;


#pragma mark Initialization
- (id) initWithMovie:(NSRect)frame movie:(NSURL*)movie {
	//NSString *str = @"NFSfinal" ;
	
	url   = movie ;
	self = [super initWithFrame:frame];
    if (self) {
		
		[self setupLayers] ;
        // Initialization code here.
    }
    return self;
}


- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		
		[self setupLayers] ;
        // Initialization code here.
    }
    return self;
}


- (void)drawRect:(NSRect)dirtyRect {
    // set any NSColor for filling
    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);
}

- (void) disapear:(float) withDuration {
	[CATransaction begin];
	[CATransaction setValue:[NSNumber numberWithFloat:withDuration] forKey:
	 kCATransactionAnimationDuration];
	[CATransaction setValue:[NSNumber numberWithBool: NO] forKey:
	 kCATransactionDisableActions];
	
	flMovieLayer.opacity = 0.0;
	
	[CATransaction commit];
	
	[flMovie stop] ;
}


- (void) reapear:(float) withDuration {
	[flMovie gotoBeginning] ;
	[flMovie play] ;
	[CATransaction begin];
	[CATransaction setValue:[NSNumber numberWithFloat:withDuration] forKey:
	 kCATransactionAnimationDuration];
	[CATransaction setValue:[NSNumber numberWithBool: NO] forKey:
	 kCATransactionDisableActions];
	
	flMovieLayer.opacity = 1.0;
	
	[CATransaction commit];
	
}


#pragma mark setup
-(void) setupLayers {
	rootLayer = [CALayer layer] ;
	rootLayer.backgroundColor = CGColorGetConstantColor(kCGColorWhite) ;
	rootLayer.bounds = CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height) ;
	[self setLayer:rootLayer] ;
	
	
	NSError *error ;
	[flMovie release] ;
	
	flMovie = [[QTMovie movieWithURL:url error:&error] retain] ;
	[flMovie setAttribute:[NSNumber numberWithInt:1] forKey:QTMovieLoopsAttribute] ;
	
	
	if (flMovie) {
		if (flMovieLayer) {
			[flMovieLayer setMovie:flMovie] ;
			//flMovieLayer.frame = CGRectMake(0, 0, 200, 200) ;
		}
		else {
			// create movie layer
			flMovieLayer = [QTMovieLayer layerWithMovie:flMovie] ;
			// size layer
			flMovieLayer.frame = CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height) ;
			//flMovieLayer.position = CGPointMake(200, 200);
			// scale the movie layer with the container (it will resize with the window)
            //flMovieLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
			[rootLayer addSublayer:flMovieLayer] ;
			
		}
	}
	// start the movie playing
	[flMovie play];
	
}


#pragma mark cleanup
- (void) dealloc {
	
	[rootLayer release] ;
	[flMovieLayer release] ;
	[flMovie release] ;
	
	[super dealloc] ;
}


@end
